//
//  Context.m
//  Pennyworth
//
//  Created by Chris Karr on 12/24/07.
//  Copyright 2007 Chris J. Karr. All rights reserved.
//

#import "Context.h"
#import "Observation.h"
#import "Learner.h"
#import "PredictionManager.h"
#import "TrainingManager.h"

@implementation Context

@synthesize observations;
@synthesize predictionTimer;

- (void) awakeFromNib
{
	[[NSUserDefaults standardUserDefaults] setBool:NO forKey:IS_TRAINING];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(process:) name:OBSERVATION_UPDATE object:nil];
	
	[[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(cleanup:) userInfo:nil repeats:YES] retain];
}

- (void) cleanup:(NSTimer *) theTimer
{
	NSMutableArray * expired = [NSMutableArray array];

	for (Observation * observation in [observations arrangedObjects])
	{
		if (observation.duration != nil)
		{
			NSTimeInterval life = [[NSDate date] timeIntervalSinceDate:observation.date];
			
			if (life > [observation.duration doubleValue])
				[expired addObject:observation];
		}
	}
	
	for (NSDictionary * observation in expired)
		[observations removeObject:observation];
}

- (void) process:(NSNotification *) note
{
	NSDictionary * dict = [note userInfo];
	
	if (dict != nil)
	{
		Observation * obs = [[Observation alloc] init];
	
		obs.sensor = [dict valueForKey:OBSERVATION_SENSOR];
		obs.observation = [dict valueForKey:OBSERVATION_OBSERVATION];
		obs.date = [NSDate date];
		obs.duration = 	[dict valueForKey:OBSERVATION_LIFESPAN];

		NSMutableArray * found = [NSMutableArray array];
	
		for (Observation * observation in [observations arrangedObjects])
		{
			if ([observation.sensor isEqualToString:obs.sensor])
				[found addObject:observation];
		}
	
		[observations removeObjects:found];
	
		if (obs.observation == nil)
			return;
		
		if ([obs.observation isKindOfClass:[NSArray class]])
		{
			if ([(NSArray *) obs.observation count] == 0)
				return;
		}

		[observations addObject:obs];

		[obs release];
	}
	
	if (self.predictionTimer == nil)
		self.predictionTimer = [NSTimer scheduledTimerWithTimeInterval:0.75 target:self selector:@selector(fetchPrediction:) userInfo:nil repeats:NO];
}

- (void) fetchPrediction:(NSTimer *) theTimer
{
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	
	if ([defaults boolForKey:PREDICTIONS_ACTIVE] && ![defaults boolForKey:IS_TRAINING])
	{
		NSMutableDictionary * userInfo = [NSMutableDictionary dictionary];
	
		[userInfo setValue:[self observationArray] forKey:FEATURE_LIST];

		[[NSNotificationCenter defaultCenter] postNotificationName:FETCH_PREDICTION object:self userInfo:userInfo];
	}
	
	[self.predictionTimer invalidate];
	self.predictionTimer = nil;
}

- (NSArray *) observationArray
{
	NSMutableArray * obsArray = [NSMutableArray array];
	
	for (Observation * obs in [self.observations arrangedObjects])
	{
		NSDictionary * obsDict = [NSDictionary dictionaryWithObjectsAndKeys:obs.sensor, FEATURE_SENSOR, 
																			obs.observation, FEATURE_OBSERVATION, nil];

		[obsArray addObject:obsDict];
	}
	
	return obsArray;
}


@end
